<?

//############

function pointInPolygonHelper($p, $poly) {
  $num = count($poly)/2;
  $inPoly = false;
  $j = $num-1;
  $v1 = 0;
  $v2 = 0;
  $px = $p[0];
  $py = $p[1];
  for ($i=0; $i<$num; $i++) {
    $v1x = $poly[2*$i];
    $v1y = $poly[2*$i+1];
    $v2x = $poly[2*$j];
    $v2y = $poly[2*$j+1];
    if (($v1y > $py) !== ($v2y > $py))
      if ($px < ($v2x-$v1x)*($py-$v1y)/($v2y-$v1y)+$v1x)
        $inPoly = !$inPoly;
    $j = $i;
  }
  return $inPoly;
}

//############

function pointInPolygon($p, $poly) {
  $outer = $poly/*[0]*/;
  if (!pointInPolygonHelper($p, $outer))
    return false;
  /*
  for (var i=1; i<poly.length; i++)
    if (pointInPolygonHelper(p, poly[i]))
      return false;
  */
  return true;
}

?>
